package com.ihd.ihardware.view.enter.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityHomeBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.viewModel.HomeModel;
import com.ihd.ihardware.view.lock.view.LockActivity;
import com.ihd.ihardware.view.lock.view.LockBluetoothActivity;
import com.ihd.ihardware.view.lock.view.LockNoBindingActivity;
import com.ihd.ihardware.view.scooter.view.SCActivity;
import com.ihd.ihardware.view.scooter.view.SCBluetoothActivity;
import com.ihd.ihardware.view.scooter.view.SCNoBindingActivity;
import com.ihd.ihardware.view.tzc.health.view.BindingActivity;
import com.ihd.ihardware.view.tzc.me.view.DeviceActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.utils.BLEUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;

public class HomeActivity extends BaseActivity<ActivityHomeBinding, HomeModel> {
    private UserInfoRes.DataBean uInfo;

    private List<DeviceListRes.DataBean.ListBean> ddls;
    private DeviceListRes.DataBean.ListBean tzcddl;
    private DeviceListRes.DataBean.ListBean hbcddl;
    private List<DeviceListRes.DataBean.ListBean> lysddl = new ArrayList<>();

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        uInfo = (UserInfoRes.DataBean) getIntent().getSerializableExtra("userinfo");

        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setTitle("我的设备");
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back_w);
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));

        viewModel.mDV0Adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                switch (position) {
                    case 0:
                        if (tzcddl != null) {
                            Intent mIntent = new Intent(HomeActivity.this, DeviceActivity.class);
                            mIntent.putExtra("device", tzcddl);
                            startActivity(mIntent);
                        } else {
                            Intent i = new Intent(HomeActivity.this, BindingActivity.class);
                            startActivity(i);
                        }
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            }
        });
        binding.lifeHealth.setAdapter(viewModel.mDV0Adapter);


        viewModel.mDV1Adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                switch (position) {
                    case 0:
                        if (!BLEUtils.getInstance().isBluetoothOpened()) {
                            Intent i = new Intent(v.getContext(), SCBluetoothActivity.class);
                            startActivity(i);
                        } else {
                            if (hbcddl == null) {
                                Intent mIntent = new Intent(HomeActivity.this, SCNoBindingActivity.class);
                                startActivity(mIntent);
                            } else {
                                Intent i = new Intent(v.getContext(), SCActivity.class);
                                i.putExtra("sc", hbcddl);
                                startActivity(i);
                            }
                        }
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            }
        });
        binding.HealthWork.setAdapter(viewModel.mDV1Adapter);


        viewModel.mDV2Adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                switch (position) {
                    case 0:
                        if (!BLEUtils.getInstance().isBluetoothOpened()) {
                            Intent i = new Intent(v.getContext(), LockBluetoothActivity.class);
                            startActivity(i);
                        } else {
                            if (lysddl.size() == 0) {
                                Intent mIntent = new Intent(HomeActivity.this, LockNoBindingActivity.class);
                                startActivity(mIntent);
                            } else {
                                Intent mIntent = new Intent(HomeActivity.this, LockActivity.class);
                                mIntent.putExtra("locks", (Serializable) lysddl);
                                startActivity(mIntent);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        });
        binding.lifeUse.setAdapter(viewModel.mDV2Adapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        lysddl.clear();
        tzcddl = null;
        hbcddl = null;
        String temp = SPUtils.getString(AppConstans.DEVICES, "");
        if (!TextUtils.isEmpty(temp)) {
            ddls = new Gson().fromJson(temp, new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
            }.getType());
            for (DeviceListRes.DataBean.ListBean d : ddls) {
                if ("0".equals(d.getType()) || "1".equals(d.getType()))
                    tzcddl = d;
                if ("2".equals(d.getType()) || "3".equals(d.getType()))
                    lysddl.add(d);
                if ("4".equals(d.getType()))
                    hbcddl = d;
            }
        } else
            ddls = new ArrayList<>();

        viewModel.getLifeHealthList(tzcddl);
        viewModel.getHealthWorkList(hbcddl);
        viewModel.getLifeUseList(lysddl);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected Class getViewModelClass() {
        return HomeModel.class;
    }

}
